package com.wxzz.elearing.pay.quartz;

import com.wxzz.elearing.pay.entity.UserOrderStatisticsData;
import com.wxzz.elearing.pay.mapper.UserOrderMapper;
import com.wxzz.elearing.pay.mapper.UserOrderStatisticsDataMapper;
import com.wxzz.elearing.pay.vo.SelectAllIncomePriceVo;
import com.wxzz.elearning.common.constants.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class OrderStatisticsQuartz {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserOrderStatisticsDataMapper userOrderStatisticsDataMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;


    //@Scheduled(cron = "*/5 * * * * ?")
    @Scheduled(cron = "0 59 23 * * ?")
    public void orderStatisticsQuartz() {
        //分布式环境下进行幂等
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(RedisConstants.ORDER_STATISTICS_DATA_QUARTZ, RedisConstants.ORDER_STATISTICS_DATA_QUARTZ, 60 * 60, TimeUnit.SECONDS);
        if (!Boolean.TRUE.equals(aBoolean)) {
            //如果已存在则直接退出任务
            return;
        }
        String startTime = LocalDate.now().toString();
        String endTime = LocalDate.now().plusDays(1).toString();
        SelectAllIncomePriceVo allCourse = userOrderMapper.selectAllIncomePrice(0, startTime, endTime);
        UserOrderStatisticsData allCourseData = new UserOrderStatisticsData();
        allCourseData.setSaveTime(startTime);
        if (allCourse != null) {
            allCourseData.setAmount(allCourse.getAmount());
            allCourseData.setOrderNum(allCourse.getOrderNum());
            allCourseData.setResultJson(allCourse.toString());
        }
        allCourseData.setModelType(0);
        userOrderStatisticsDataMapper.insert(allCourseData);
        log.info("统计平台总课程销量:{}", allCourseData.toString());


        SelectAllIncomePriceVo allLive = userOrderMapper.selectAllIncomePrice(1, startTime, endTime);
        UserOrderStatisticsData allLiveData = new UserOrderStatisticsData();
        allLiveData.setSaveTime(startTime);
        if (allLive != null) {
            allLiveData.setAmount(allLive.getAmount());
            allLiveData.setOrderNum(allLive.getOrderNum());
            allLiveData.setResultJson(allLive.toString());
        }
        allLiveData.setModelType(1);
        userOrderStatisticsDataMapper.insert(allLiveData);


        SelectAllIncomePriceVo allOnActivity = userOrderMapper.selectAllIncomePrice(2, startTime, endTime);
        UserOrderStatisticsData allOnActivityData = new UserOrderStatisticsData();
        allOnActivityData.setSaveTime(startTime);
        if (allOnActivity != null) {
            allOnActivityData.setAmount(allOnActivity.getAmount());
            allOnActivityData.setOrderNum(allOnActivity.getOrderNum());
            allOnActivityData.setResultJson(allOnActivity.toString());
        }
        allOnActivityData.setModelType(2);
        userOrderStatisticsDataMapper.insert(allOnActivityData);


        SelectAllIncomePriceVo allAnswers = userOrderMapper.selectAllIncomePrice(3, startTime, endTime);
        UserOrderStatisticsData allAnswersData = new UserOrderStatisticsData();
        allAnswersData.setSaveTime(startTime);
        if (allAnswers != null) {
            allAnswersData.setAmount(allAnswers.getAmount());
            allAnswersData.setOrderNum(allAnswers.getOrderNum());
            allAnswersData.setResultJson(allAnswers.toString());
        }
        allAnswersData.setModelType(3);
        userOrderStatisticsDataMapper.insert(allAnswersData);


        SelectAllIncomePriceVo allReward = userOrderMapper.selectAllIncomePrice(4, startTime, endTime);
        UserOrderStatisticsData allRewardData = new UserOrderStatisticsData();
        allRewardData.setSaveTime(startTime);
        if (allReward != null) {
            allRewardData.setAmount(allReward.getAmount());
            allRewardData.setOrderNum(allReward.getOrderNum());
            allRewardData.setResultJson(allReward.toString());
        }
        allRewardData.setModelType(4);
        userOrderStatisticsDataMapper.insert(allRewardData);


        SelectAllIncomePriceVo allOffActivity = userOrderMapper.selectAllIncomePrice(5, startTime, endTime);
        UserOrderStatisticsData allOffActivityData = new UserOrderStatisticsData();
        allOffActivityData.setSaveTime(startTime);
        if (allOffActivity != null) {
            allOffActivityData.setAmount(allOffActivity.getAmount());
            allOffActivityData.setOrderNum(allOffActivity.getOrderNum());
            allOffActivityData.setResultJson(allOffActivity.toString());
        }
        allOffActivityData.setModelType(5);
        userOrderStatisticsDataMapper.insert(allOffActivityData);


    }

}
